package com.tanhua.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 查询用户详情根据环信账号
     *
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity userinfo(String huanxinId) {
        UserInfoVo vo = messagesService.findUserInfoByHuanxin(huanxinId);
        return ResponseEntity.ok(vo);
    }

    /**
     * 将用户添加到联系人
     *
     * @param
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity<Void> contactsTo(@RequestBody Map map) {
        String userId = map.get("userId").toString();
        messagesService.contactsTo(Long.valueOf(userId));
        return ResponseEntity.ok(null);
    }

    /**
     * 查询联系人目录
     * @param page
     * @param pagesize
     * @param keyword 用户昵称
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity<PageResult> getContacts(@RequestParam(defaultValue = "1") Integer page,
                                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                                  String keyword) {
        PageResult pageResult = messagesService.getContacts(page, pagesize, keyword);
        return ResponseEntity.ok(pageResult);
    }


}
